package com.star.common.orm.entity;

import cn.hutool.core.collection.CollUtil;
import com.star.common.orm.repository.RepositoryRegistry;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 基础模型
 *
 * @author zhouhengzhe
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public abstract class Model implements Serializable {
    @ApiModelProperty("主键ID")
    private Long id;

    @ApiModelProperty("创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty("更新时间")
    private LocalDateTime updateTime;

    public boolean save() {
        return RepositoryRegistry.get(this.getClass()).save(this);
    }

    public boolean update() {
        return RepositoryRegistry.get(this.getClass()).update(this);
    }

    public boolean update(QueryParam queryParam) {
        return RepositoryRegistry.get(queryParam.getClass()).update(this, queryParam);
    }

    public boolean updateByKey() {
        return RepositoryRegistry.get(this.getClass()).updateByKey(this);
    }

    public boolean saveOrUpdate() {
        return RepositoryRegistry.get(this.getClass()).saveOrUpdate(this);
    }

    public boolean delete() {
        return RepositoryRegistry.get(this.getClass()).delete(this.getId());
    }

    public <Q extends QueryParam> void fill(Q queryParam) {
        RepositoryRegistry.get(this.getClass()).fill(queryParam, Collections.singletonList(this));
    }

    public static boolean save(List<? extends Model> models) {
        if (CollUtil.isEmpty(models)) {
            return false;
        }
        return RepositoryRegistry.get(models.get(0).getClass()).save(models);
    }

    public static boolean update(List<? extends Model> models) {
        if (CollUtil.isEmpty(models)) {
            return false;
        }
        return RepositoryRegistry.get(models.get(0).getClass()).update(models);
    }


    public static boolean updateByKey(List<? extends Model> models) {
        if (CollUtil.isEmpty(models)) {
            return false;
        }
        return RepositoryRegistry.get(models.get(0).getClass()).updateByKey(models);
    }

    public static <M extends Model> M getOne(QueryParam queryParam) {
        return (M) RepositoryRegistry.get(queryParam.getClass()).getOne(queryParam);
    }

    public static long count(QueryParam queryParam) {
        return RepositoryRegistry.get(queryParam.getClass()).count(queryParam);
    }

    public static boolean exist(QueryParam queryParam) {
        return RepositoryRegistry.get(queryParam.getClass()).exist(queryParam);
    }

    public static <M extends Model> M first(QueryParam queryParam) {
        return (M) RepositoryRegistry.get(queryParam.getClass()).first(queryParam);
    }

    public static boolean delete(QueryParam queryParam) {
        return RepositoryRegistry.get(queryParam.getClass()).delete(queryParam);
    }

    public static <M extends Model> Page<M> page(QueryParam queryParam) {
        return (Page<M>) RepositoryRegistry.get(queryParam.getClass()).page(queryParam);
    }

    public static <M extends Model> List<M> list(QueryParam queryParam) {
        return (List<M>) RepositoryRegistry.get(queryParam.getClass()).list(queryParam);
    }
}